package com.deYang.app.ljodfh.ui.utils;

import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;

/**
 * Created by liuyu on 2017/10/11.
 */

public class LiveItemImagTarget extends GlideDrawableImageViewTarget {

    private float mxW = -1F;

    private String tag;

    private int p=-1;

    public LiveItemImagTarget(ImageView view) {
        super(view);
    }

    @Override
    public void onStart() {
        if (view.getWidth() == 0 || view.getHeight() == 0) {
            super.onStart();
        }
//        JLog.e("ajjj  tag="+tag+"   resource=");
    }

    @Override
    public void onLoadStarted(Drawable placeholder) {
        p++;
//        JLog.e("ajjj  qqsq  tag="+tag+"   resource="+placeholder+"  "+p+"  "+view);
        super.onLoadStarted(placeholder);
    }

    @Override
    public void onLoadFailed(Exception e, Drawable errorDrawable) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        boolean b = false;
        if (lp.width > 0) {
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            b = true;
        }
        if (lp.height > 0) {
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            b = true;
        }
        if (b) {
            view.setLayoutParams(lp);
        }
        super.onLoadFailed(e, errorDrawable);
    }

    @Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
        int vw = view.getWidth();
        float vh = view.getHeight();
        float riw = resource.getIntrinsicWidth();
        float rih = resource.getIntrinsicHeight();
//        JLog.e("ajjj  riw=" + riw + "  rih=" + rih + "  rmw=" + resource.getMinimumWidth() + "   " + tag
//                + "  rmh=" + resource.getMinimumHeight() + "\nvw=" + vw + "  vh=" + vh + "  " + view.getMaxHeight()
//                + "   " + view.getMaxWidth() + "   " + this);
        int q = 0;
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        float fx = riw / rih;
        if (fx >= 5 || fx <= 0.2) {
            q=1;
            setScaleType(ImageView.ScaleType.CENTER_CROP);
            lp.width = (int) (0.75 * view.getMaxHeight());
        } else {
            setScaleType(ImageView.ScaleType.FIT_XY);
            if (fx >= 1) {
                q=22;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.height = (int) (mxW / riw * rih);
            } else {
                q=23;
                lp.width = (int) (mxW / 2);
                lp.height = (int) (lp.width / riw * rih);
            }
        }
        if (lp != null) {
            view.setLayoutParams(lp);
        }
        super.onResourceReady(resource, animation);
//        JLog.e("ajjj  lp=" + lp + "   q=" + q + "   mxw=" + mxW + "   " + tag);
//        if (view != null) {
//            JLog.e("ajjj will requestLayout  ");
//            view.requestLayout();
//        }
    }

    private void setScaleType(ImageView.ScaleType type) {
        if (view == null || type == null) {
            return;
        }
        if (view.getScaleType() != type) {
            view.setScaleType(type);
        }
    }

    public void setMxW(float mxW) {
        if (mxW <= 0) {
            this.mxW = -1;
        } else {
            this.mxW = mxW;
        }
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    private void temp() {
//        if (vw > 0 && (Math.abs(vw - riw) > 10 || Math.abs(vh - rih) > 10)) {
//            ViewGroup.LayoutParams lp = view.getLayoutParams();
//            q = 101;
//            float fx = 0;
//            if (vh >= rih) {
//                setScaleType(ImageView.ScaleType.FIT_XY);
//                q = 111;
//                fx = vw - riw;
//                lp.height = (int) rih;
//                if (fx > 10) {
//                    q = 112;
//                    lp.width = (int) riw;
//                } else if (fx < -10) {
//                    q = 1131;
//                    if (mxW > 0 && riw <= mxW) {
//                        q = 1132;
//                        lp.width = (int) riw;
//                    } else if (lp.width > 0) {
//                        q = 1133;
//                        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    } else {
//                        q = 1134;
//                        lp = null;
//                    }
//                } else {
//                    q = 114;
//                    lp = null;
//                }
//            } else {
//                q = 121;
//                fx = rih - view.getMaxHeight();
//                if (fx > 10) {
//                    q = 1211;
//                    lp.height = view.getMaxHeight();
//                    if (rih / riw >= 5) {
//                        setScaleType(ImageView.ScaleType.CENTER_CROP);
//                        lp.width = (int) (0.75 * view.getMaxHeight());
//                    } else {
//                        setScaleType(ImageView.ScaleType.FIT_XY);
//                        int w = (int) (riw / rih * view.getMaxHeight());
//                        if (mxW > 0 && w <= mxW) {
//                            q = 1213;
//                            lp.width = w;
//                        } else if (lp.width > 0) {
//                            q = 1214;
//                            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                        }
//                    }
//                } else if (fx < -10) {
//                    setScaleType(ImageView.ScaleType.FIT_XY);
//                    lp.height = (int) rih;
//                    q = 1221;
//                    if (mxW > 0 && riw <= mxW) {
//                        q = 1222;
//                        lp.width = (int) riw;
//                    } else if (lp.width > 0) {
//                        q = 1223;
//                        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    }
//                } else {
//                    q = 131;
//                    lp = null;
//                }
//            }
//            if (lp != null) {
//                view.setLayoutParams(lp);
//            }
//            JLog.e("ajjj  lp=" + lp + "   q=" + q + "   mxw=" + mxW + "   " + tag);
//        }
    }
}
